package edu.uoc.tdp.pac4.client.pantalles;


import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.EtchedBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Frame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.util.ArrayList;
import java.util.List;

import edu.uoc.tdp.pac4.common.beans.ComboItem;
import edu.uoc.tdp.pac4.common.beans.S1LlistaPerfils;
import edu.uoc.tdp.pac4.common.beans.S1Taller;
import edu.uoc.tdp.pac4.common.beans.S1Usuari;
import edu.uoc.tdp.pac4.common.exception.ExcepcioLogin;
import edu.uoc.tdp.pac4.common.interfaces.S1Interficie;
import edu.uoc.tdp.pac4.common.utils.TDSLanguageUtils;
//import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;



/**
 * Classe de la pantalla que dona d'alta un usuari
 * @author Josep Garcia
 *
 */
public class S1PantallaAltaUsuari extends JDialog {

	private final String NL = System.getProperty("line.separator");

	private final JPanel contentPanel = new JPanel();
	private JButton botoDesar;
	private JButton botoCancel;
	private JPanel buttonPane;

	private JTextField tfDni;
	private JTextField tfNom;
	private JTextField tfCognoms;

	private JPasswordField pwdField1;
	private JPasswordField pwdField2;
	private JTextField tfUsername;

	
	private S1Interficie objecteRemot;
	private S1Usuari usuari;
	
	private JComboBox comboTallers;
	private JComboBox comboPerfils;

	private S1LlistaPerfils llistaPerfils;
	
	private List<S1Taller> tallersLst;
	private JTextField tfIdUsuari;
	private JTextField tfAlta;
	private JTextField tfModifica;
	private JTextField tfBaixa;
	private JCheckBox checkEstaActiu;

	private Frame framePare;

	/**
	 * Create la pantalla d'alta d'un usuari
	 */
	public S1PantallaAltaUsuari(Frame pFrame, S1Interficie objRemot){

		super(pFrame, "Smart Repair", true);
		setResizable(false);
		objecteRemot = objRemot;
		
		framePare = pFrame;
		
		iniciaComponents();
		
		carregaComboPerfils();
		carregaComboTallers();
		
		this.setLocationRelativeTo(getParent());
		setVisible(true);
	}
	

	/**
	 * Metode que inserta un usuari en la Base de Dades
	 */
	private void insertarUsuari(){
		
		try{
			// si no coincideixen el pwd nou i la seva repeticio -> error
			if (!String.valueOf(pwdField1.getPassword()).equals(String.valueOf(pwdField2.getPassword()))){
				throw new ExcepcioLogin(ExcepcioLogin.ERROR_PWD_NOUS_NO_COINCIDENTS);				
			}
			usuari = new S1Usuari();
			usuari.setDni(tfDni.getText());
			usuari.setNom(tfNom.getText());
			usuari.setCognoms(tfCognoms.getText());
			usuari.setPerfil(comboPerfils.getSelectedItem().toString());
			
			//usuari.setTaller(comboTallers.getSelectedIndex());  < ---- MALAMENT 
			usuari.setTaller(((ComboItem) comboTallers.getSelectedItem()).value());
			
			usuari.setActiu(checkEstaActiu.isSelected());
			usuari.setUsername(tfUsername.getText());
			usuari.setPassword(String.valueOf(pwdField1.getPassword()));

			if (objecteRemot.insertarUsuari(usuari)){
				
				JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("miss.insertat.ok"), "SmartRepair", JOptionPane.INFORMATION_MESSAGE);

				dispose();
			}
		}
		catch (Exception e){
			
			JOptionPane.showMessageDialog(this.getParent(), e.getMessage(), "SmartRepair", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
	

	
	/**
	 * Carrega el combo dels tallers des de la taula tallers
	 * @throws  
	 */
	private void carregaComboTallers(){

		tallersLst = new ArrayList<S1Taller>();
		comboTallers.addItem(new ComboItem(-1, TDSLanguageUtils.getMessage("selleccionar.taller")));

		try{
			tallersLst = objecteRemot.getLlistaTallerFiltre("FILTRE_NULL", "null"); //objecteRemot.getTallersLst();

			for(S1Taller taller:tallersLst){
				
				comboTallers.addItem(new ComboItem(taller.getIdTaller(), taller.getNom()));
			}
		}
		catch (Exception e){
			JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("ERROR_EXCEPCIO")
							+ NL  +e.getMessage(), "SmartRepair", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}

	
	
	
	/**
	 * Carrega el combo dels perfils del usuaris
	 * @throws  
	 */
	private void carregaComboPerfils(){
			
		llistaPerfils = new S1LlistaPerfils();
		comboPerfils.addItem(new ComboItem(-1, TDSLanguageUtils.getMessage("selleccionar.perfil")));
		
		int it = 0;
		for (String perf:llistaPerfils.getLlistaPerfils()){
			
			comboPerfils.addItem(new ComboItem(it, perf));
			
			it++;
		}
	}


	/**
	 * Metode que inicia els components de la pantalla Alta Usuari
	 */
	private void iniciaComponents(){
		setBounds(100, 100, 699, 400);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		JPanel panel = new JPanel();
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		{
			buttonPane = new JPanel();
			buttonPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		}
		{
			botoCancel = new JButton(TDSLanguageUtils.getMessage("boto.cancel"));
			botoCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
			botoCancel.setActionCommand("Cancel");
		}
		
		JLabel lblidtaller = new JLabel(TDSLanguageUtils.getMessage("etiqueta.idusuari"));
		lblidtaller.setVisible(false);
		
		tfIdUsuari = new JTextField();
		tfIdUsuari.setVisible(false);
		tfIdUsuari.setColumns(10);
		tfIdUsuari.setEnabled(false);
		
		JLabel lblDni = new JLabel(TDSLanguageUtils.getMessage("etiqueta.dni"));
		
		tfDni = new JTextField();
		tfDni.setColumns(9);
		
		JLabel lblUsername = new JLabel(TDSLanguageUtils.getMessage("etiqueta.username"));
		
		tfUsername = new JTextField();
		tfUsername.setColumns(10);
		
		JLabel lblNom = new JLabel(TDSLanguageUtils.getMessage("etiqueta.nom"));
		
		tfNom = new JTextField();
		tfNom.setColumns(20);
		
		JLabel lblPwd1 = new JLabel(TDSLanguageUtils.getMessage("etiqueta.pwd.nova"));
		
		pwdField1 = new JPasswordField();
		pwdField1.setColumns(10);
		
		JLabel lblCognoms = new JLabel(TDSLanguageUtils.getMessage("etiqueta.cognoms"));
		
		tfCognoms = new JTextField();
		tfCognoms.setColumns(40);
		
		JLabel lblPwd2 = new JLabel(TDSLanguageUtils.getMessage("etiqueta.pwd.repetir"));
		
		pwdField2 = new JPasswordField();
		pwdField2.setColumns(10);
		
		JLabel lblperfil = new JLabel(TDSLanguageUtils.getMessage("etiqueta.perfil"));
		
		JLabel lblTaller = new JLabel(TDSLanguageUtils.getMessage("etiqueta.taller"));
		
		comboTallers = new JComboBox();
		
		comboPerfils = new JComboBox();
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(contentPanel, GroupLayout.PREFERRED_SIZE, 694, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(contentPanel, GroupLayout.PREFERRED_SIZE, 372, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(35, Short.MAX_VALUE))
		);
		{
			botoDesar = new JButton(TDSLanguageUtils.getMessage("boto.desar"));
			botoDesar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					insertarUsuari();
				}
			});
			botoDesar.setActionCommand("OK");
			getRootPane().setDefaultButton(botoDesar);
		}
		GroupLayout gl_buttonPane = new GroupLayout(buttonPane);
		gl_buttonPane.setHorizontalGroup(
			gl_buttonPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_buttonPane.createSequentialGroup()
					.addGap(228)
					.addComponent(botoCancel)
					.addGap(45)
					.addComponent(botoDesar)
					.addContainerGap(223, Short.MAX_VALUE))
		);
		gl_buttonPane.setVerticalGroup(
			gl_buttonPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_buttonPane.createSequentialGroup()
					.addGap(14)
					.addGroup(gl_buttonPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(botoCancel)
						.addComponent(botoDesar))
					.addContainerGap(10, Short.MAX_VALUE))
		);
		buttonPane.setLayout(gl_buttonPane);
		
		JLabel lblestaActiu = new JLabel(TDSLanguageUtils.getMessage("etiqueta.esta.actiu"));
		
		checkEstaActiu = new JCheckBox("");
		
		JLabel lbldataAlta = new JLabel(TDSLanguageUtils.getMessage("etiqueta.data.alta"));
		lbldataAlta.setVisible(false);
		
		tfAlta = new JTextField();
		tfAlta.setVisible(false);
		tfAlta.setColumns(10);
		tfAlta.setEnabled(false);
		
		JLabel lbldataModificacio = new JLabel(TDSLanguageUtils.getMessage("etiqueta.data.modificacio"));
		lbldataModificacio.setVisible(false);
		
		tfModifica = new JTextField();
		tfModifica.setVisible(false);
		tfModifica.setColumns(10);
		tfModifica.setEnabled(false);
		
		JLabel lbldataBaixa = new JLabel(TDSLanguageUtils.getMessage("etiqueta.data.baixa"));
		lbldataBaixa.setVisible(false);
		
		tfBaixa = new JTextField();
		tfBaixa.setVisible(false);
		tfBaixa.setColumns(10);
		tfBaixa.setEnabled(false);
		
		JLabel lblmodificarUsuari = new JLabel(TDSLanguageUtils.getMessage("etiqueta.alta.usuari"));
		lblmodificarUsuari.setHorizontalAlignment(SwingConstants.CENTER);
		
		
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblmodificarUsuari, GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
							.addContainerGap())
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(59)
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
										.addComponent(lblidtaller)
										.addComponent(lblDni)
										.addComponent(lblNom)
										.addComponent(lblCognoms)
										.addComponent(lblperfil))
									.addPreferredGap(ComponentPlacement.UNRELATED))
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
										.addComponent(lblTaller)
										.addComponent(lblestaActiu)
										.addComponent(lbldataAlta))
									.addGap(10)))
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(comboPerfils, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(comboTallers, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(tfCognoms, 0, 0, Short.MAX_VALUE)
										.addComponent(tfNom, 0, 0, Short.MAX_VALUE)
										.addComponent(tfDni)
										.addComponent(tfAlta, GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
										.addComponent(tfIdUsuari))
									.addPreferredGap(ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(lbldataBaixa)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(tfBaixa, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(lbldataModificacio)
											.addGap(10)
											.addComponent(tfModifica, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel.createSequentialGroup()
											.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
												.addComponent(lblUsername)
												.addComponent(lblPwd1)
												.addComponent(lblPwd2))
											.addGap(10)
											.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
												.addComponent(pwdField2, Alignment.LEADING)
												.addComponent(tfUsername, Alignment.LEADING)
												.addComponent(pwdField1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE))))
									.addGap(94))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(checkEstaActiu)
									.addContainerGap())))))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(7)
					.addComponent(lblmodificarUsuari)
					.addGap(16)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(tfIdUsuari, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblidtaller))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfDni, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblDni))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfNom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNom))
							.addGap(10)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfCognoms, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblCognoms))
							.addGap(17)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblperfil)
								.addComponent(comboPerfils, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(7)
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblestaActiu)
								.addComponent(checkEstaActiu))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(comboTallers, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblTaller))
							.addGap(18)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfAlta, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lbldataAlta)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblUsername)
								.addComponent(tfUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblPwd1)
								.addComponent(pwdField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(pwdField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblPwd2))
							.addGap(75)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfModifica, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lbldataModificacio))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfBaixa, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lbldataBaixa))))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
				.addComponent(buttonPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 301, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(buttonPane, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
					.addGap(30))
		);
	//	panel.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{tfDni, tfNom, tfCognoms, comboPerfils, checkEstaActiu, comboTallers, tfUsername, pwdField1, pwdField2}));
		contentPanel.setLayout(gl_contentPanel);
		getContentPane().setLayout(groupLayout);
	}
}
